import time

from selenium import webdriver


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    # driver: WebDriver
    base_url = ""
    select_element: WebElement = None

    def __init__(self, driver: WebDriver = None):
        if driver == None:
            # 第一次初始化
            options = Options()
            # options.debugger_address = "127.0.0.1:9222"
            self.driver = webdriver.Chrome(options=options)
            self.driver.maximize_window()
            self.driver.implicitly_wait(5)
        else:
            # 进行页面跳转的操作
            self.driver = driver

        # base_url 打开某个页面
        if self.base_url != "":
            self.driver.get(self.base_url)

    def find(self, locator: tuple, timeout=10):
        method: By
        if locator[0] == 'id':
            method = By.ID
        elif locator[0] == 'xpath':
            method = By.XPATH
        elif locator[0] == 'name':
            method = By.NAME
        else:
            assert '暂时没封装这个方法,自己写吧'
        if self.select_element != None:
            self.driver.execute_script("arguments[0].setAttribute('style','')", self.select_element)
        element: WebElement = WebDriverWait(self.driver, timeout).until(
            expected_conditions.element_to_be_clickable((method, locator[1])))
        self.select_element = element
        self.driver.execute_script("arguments[0].setAttribute('style','box-shadow: 0 0 10px 10px red')",
                                   self.select_element)
        return element

    def click_ele(self, locator: tuple, timeout=10):
        self.find(locator, timeout).click()

    def send_ele(self, locator: tuple, content: str, timeout=10):
        self.find(locator, timeout).send_keys(content)

    def finds(self, by, locator):
        return self.driver.find_elements(by, locator)

    def wait_for_click(self, locator, timeout=10):
        element: WebElement = WebDriverWait(self.driver, timeout).until(
            expected_conditions.element_to_be_clickable(locator))
        return element
    #
    # def wait_find(self, by, place, timeout=10):
    #     if self.select_element != None:
    #         self.driver.execute_script("arguments[0].setAttribute('style','')", self.select_element)
    #     element: WebElement = WebDriverWait(self.driver, timeout).until(
    #         expected_conditions.element_to_be_clickable((by, place)))
    #     self.select_element = element
    #     self.driver.execute_script("arguments[0].setAttribute('style','box-shadow: 0 0 10px 10px red')",
    #                                self.select_element)
    #     return element


    # def find_by_ID(self, ID):
    #     # return self.find(By.ID, ID)
    #     return self.wait_find(By.ID, ID)
    #
    # def find_by_name(self, name):
    #     # return self.find(By.NAME, name)
    #     return self.wait_find(By.NAME, name)
    #
    # def find_by_xpath(self, xpath):
    #     # return self.find(By.XPATH, xpath)
    #     return self.wait_find(By.XPATH, xpath)